package cn.mysdp.biz.facade;


import cn.mysdp.biz.convert.*;
import cn.mysdp.biz.domain.*;
import cn.mysdp.biz.dto.request.*;
import cn.mysdp.biz.dto.response.*;
import cn.mysdp.biz.repository.*;
import cn.mysdp.biz.facade.*;

import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import com.alibaba.fastjson.JSONObject;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.transaction.annotation.Transactional;

/**
 * @ClassName: SdpHistoryFacadeBaseImpl
 * @Description:
 * @Author: SDP
 * @Date: 2021-10-30
 * @Version: 1.0
 * Table: sdp_history
 * Comment:
 * 
 */
public class SdpHistoryFacadeBaseImpl extends BaseServiceImpl implements SdpHistoryFacade {
    @Autowired
    protected SdpHistoryMapper sdpHistoryMapper;

    public static Map<String, String> uniqueFieldMap = initUniqueFieldMap();

    public static Map initUniqueFieldMap() {
        HashMap map = new HashMap<>();
        return map;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer addSdpHistory(SdpHistoryAddRequest request) throws Exception {
        SdpHistoryWithBLOBs daoRequest = new SdpHistoryAddRequestConvert().convert(request);
        fixEqualCondition(request, "sdp_history", "add", "", null);
        Integer result = 0;
        try {
            result = sdpHistoryMapper.insertSelective(daoRequest);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            result = daoRequest.getId();
        }
        addHistory(request, "A", "sdp_history", "api", result, null);
        return result;
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer updateSdpHistory(SdpHistoryUpdateRequest request) throws Exception {
        fixEqualCondition(request, "sdp_history", "update", request.getId()+"", null);
        checkInternalId(request, "sdp_history", "update", request.getId());
        addHistory(request, "U", "sdp_history", "api", request.getId(), null);

        SdpHistoryWithBLOBs daoRequest = new SdpHistoryUpdateRequestConvert().convert(request);
        SdpHistoryExample example = new SdpHistoryExample();
        example.createCriteria().andIdEqualTo(request.getId());
        if (request.getQueryOptions() != null && request.getQueryOptions().containsKey("setnull")) {
            example.setSetNull(JSONObject.toJavaObject(request.getQueryOptions().getJSONObject("setnull"), SdpHistoryExample.CriteriaString.class));
        }
        Integer result = 0;
        try {
            result = sdpHistoryMapper.updateByExampleSelective(daoRequest, example);
        }catch(Exception ex) {
            ex.printStackTrace();
            throw checkDBDupException(ex, uniqueFieldMap);
        }
        if (result > 0) {
            return result;
        }
        throw createUpdateFailException();
    }

    @Transactional(rollbackFor = Exception.class)
    @Override
    public Integer deleteSdpHistory(SdpHistoryDeleteRequest request) throws Exception {
        Date now = new Date();
        Integer count = 0;

        BaseRequest abstractRequest = new BaseRequest();
        copyProperties(request, abstractRequest);

        for(int index = 0; index < request.getIdList().size(); index++) {
            fixEqualCondition(request, "sdp_history", "delete", request.getIdList().get(index)+"", null);
            checkInternalId(request, "sdp_history", "delete", request.getIdList().get(index));
            addHistory(abstractRequest, "D", "sdp_history", "api", request.getIdList().get(index), null);
            sdpHistoryMapper.deleteByPrimaryKey(request.getIdList().get(index));
            count++;
        }
        return count;
    }

    @Override
    public SdpHistoryQueryResponse querySdpHistory(SdpHistoryQueryRequest request) throws Exception {
        SdpHistoryWithBLOBs daoResponse = sdpHistoryMapper.selectByPrimaryKeyWithBLOBs(request.getId());
        SdpHistoryQueryResponse response = new SdpHistoryQueryResponse();
        copyProperties(daoResponse, response);
        return response;
    }

    @Override
    public List<SdpHistoryQueryResponse> listSdpHistory(SdpHistoryQueryRequest request) throws Exception {
        SdpHistoryWithBLOBs daoRequest = new SdpHistoryWithBLOBs();
        copyProperties(request, daoRequest);
        SdpHistoryExample example;
        try {
            example = SdpHistoryExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        List<SdpHistoryWithBLOBs> list;
        try {
            list = sdpHistoryMapper.selectByExampleWithBLOBs(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        List<SdpHistoryQueryResponse> responseList = new ArrayList<>();
        for(SdpHistoryWithBLOBs item : list) {
            SdpHistoryQueryResponse newItem = new SdpHistoryQueryResponse();
            copyProperties(item, newItem);
            responseList.add(newItem);
        }

        return responseList;
    }

    @Override
    public Integer countSdpHistory(SdpHistoryQueryRequest request) throws Exception {
        SdpHistoryWithBLOBs daoRequest = new SdpHistoryWithBLOBs();
        copyProperties(request, daoRequest);
        SdpHistoryExample example;
        try {
            example = SdpHistoryExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }

        Integer count;
        try {
            count = sdpHistoryMapper.countByExample(example);
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createDatabaseFailException(ex);
        }

        return count;
    }

    @Override
    public BaseStatResponse statSdpHistory(SdpHistoryQueryRequest request) throws Exception {
        throw createFailException("not implement");
    }

    @Override
    public Integer clear(SdpHistoryForClearRequest request) throws Exception {
        SdpHistoryForClear daoRequest = new SdpHistoryForClear();
        copyProperties(request, daoRequest);
        
        SdpHistoryForClearExample example;
        try {
            example = SdpHistoryForClearExample.createExample(daoRequest, request.getQueryOptions());
        } catch (Exception ex) {
            ex.printStackTrace();
            throw createFailException(ex);
        }
        
        return sdpHistoryMapper.clear(example);
    }

}
